set more off
clear all

cap log close
log using an_did_pat_ind,t replace

*** Program to compute TFP response to patent use, using industry-level TFP estimates
*** Results at the industry level and the aggregate level
*** BHH revised October 2019

use sample_clean, clear
xtset id year
keep id year firstyr_pat pat* log* dlog* tfp* ind foreign

*** Number of old patent users deleted.
count if patold & id~=id[_n-1]
drop if patold /* Analysis excludes long term patent users */
drop patold

gen trend = year-1995
rename firstyr_pat firstyr
gen control = firstyr==0
drop if firstyr<1995 & ~control  /* if first year of patenting is before 1995, old patenter */

tab pat
tab firstyr patnew 

*** Count the firms with first-time use
egen x = sum(patnew),by(id)
gen first_time = x>0
tab first_time if id~=id[_n-1]
drop x first_time

*** Lag between current year and first patent use
gen lag = year-firstyr
replace lag = 0 if lag==.  /* controls */

gen ttrend = (~control)*trend
gen before = firstyr>=year 
gen after = firstyr<year 
gen atrend = lag*after
gen btrend = before*lag
tab before after

tab ind after,miss

tab year,gen(y_)
global yrdum y_3-y_11

tab lag,gen(L_)
global lagdum L_2 L_3 L_4 L_5 L_6 L_7 L_8 L_9 L_10 L_12 L_13 L_14 L_15 L_16 L_17 L_18 L_19 L_20 L_21
replace L_11 = 0 if control

*** Regressions for TFP variables with a dummy for post-patent use

matrix btemp1 = J(9,1,0)
matrix btemp2 = J(9,1,0)
global slist logs loge loga logm tfp_acf tfp_acf_i
tabstat $slist, by(year)

foreach var of varlist $slist { 
  qui xtreg `var' after $yrdum , robust fe
  est store `var'all
  qui xtreg `var' btrend atrend, robust fe
  est store `var'5
  qui xtreg `var' $lagdum $yrdum, robust fe
  est store `var'6
 	matrix beta = (get(_b))'
	matrix btemp1 = beta["L_2".."L_10","y1"]
	matrix btemp2 = beta["L_12".."L_21","y1"]
	matrix b`var' = J(20,1,0)
	matrix b`var' = btemp1\0\btemp2
  }
esttab *all, keep(after)              ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps

esttab *5, keep(btrend atrend) ///
       b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps
esttab *6, keep($lagdum) ///
       b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps 

/*** Lag coefficients for graphs	   
esttab *6, keep($lagdum) ///
       b(%10.4f) not nostar stats(r2 rmse F df_m N N_clust) nogaps wide   
matrix LAG = J(20,6,0)
matrix LAG = blogs,bloge,bloga,blogm,btfp_acf,btfp_acf_i
matrix colnames LAG = llogs lloge lloga llogm ltfp_acf ltfp_acf_i
matrix rownames LAG = l2 l3 l4 l5 l6 l7 l8 l9 l10 l11 l12 l13 l14 l15 l16 l17 l18 l19 l20 l21
svmat LAG, names(col)
replace lag = _n-10   /*  timing variable  */
keep if lag>-6 & lag<6 & lag~=.
scatter llog* ltfp* lag, connect(l l l l l l) msymbol(i i i i i i) ///
       title("Growth versus controls around first patent use")
*/

foreach num of numlist 15 17 18 20 22 24 25 26 28 29 31 34 36 {
  foreach var of varlist $slist { 
  qui xtreg `var' after $yrdum if ind==`num', robust fe
  est store `var'ind`num'
  *qui xtreg `var' btrend atrend, robust fe
  *est store `var'5
  *qui xtreg `var' $lagdum $yrdum, robust fe
  *est store `var'6
  }
  esttab *ind`num', keep(after)              ///
         b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps
}  

log close
